import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import './assets/styles/base.css'
import '@/assets/styles/el-reset.css'
import './assets/iconfont/iconfont.css'
import qfSubMenu from "qf-sub-menu"
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'

Vue.use(ElementUI)
Vue.use(qfSubMenu)
router.beforeEach((to, from, next) => {
  NProgress.set(0.99)
  let token = localStorage.getItem('wf-token') || null;
  if (token) {
    if (store.state.menuList.length == 0) {
      store.dispatch('FETCH_MENULIST')
        .then(() => {
          next({ path: to.path })
        })
    } else {
      next()
    }

  } else {
    if (to.path === "/login") {
      next()
    } else {
      next({ path: "/login" })
    }
  }
})

router.afterEach((to, from) => {
  let crumblist = to.matched.slice(1)
  console.log(crumblist);
  store.commit("SET_CRUMBS", crumblist)
  NProgress.done()
})

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')
